//
//  AlbumView.m
//  MIXMT
//
//  Created by 方希罕 on 16/5/19.
//  Copyright © 2016年 FGL. All rights reserved.
//

#import "AlbumView.h"
#import "PHKitTool.h"
#import "AlbumCollectionViewCell.h"

@interface AlbumView ()

<
    UICollectionViewDelegate,
    UICollectionViewDataSource,
    PHPhotoLibraryChangeObserver
>

@property (nonatomic , strong)NSMutableArray *selectedArr;
@property (nonatomic , assign)NSInteger selectedCount;
@end

@implementation AlbumView

- (instancetype)initWithFrame:(CGRect)frame {

    self = [super initWithFrame:frame];
    if (self) {
        [self createAlbumCollectionView];

    }
    return self;
}

- (void)setImageArray:(NSArray *)imageArray{
    _imageArray = imageArray;
    self.selectedArr = [NSMutableArray arrayWithCapacity:self.imageArray.count];
    for (int i = 0; i < self.imageArray.count; i ++) {
        [self.selectedArr addObject:[NSNumber numberWithBool:NO]];
    }
}

- (void)createAlbumCollectionView {

    
    UICollectionViewFlowLayout *albumFlowLayout = [[UICollectionViewFlowLayout alloc] init];
    albumFlowLayout.itemSize = CGSizeMake((self.frame.size.width - 6) / 4, (self.frame.size.width - 6) / 4);
    albumFlowLayout.minimumLineSpacing = 2;
    albumFlowLayout.minimumInteritemSpacing = 2;
    albumFlowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    
    self.albumCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height) collectionViewLayout:albumFlowLayout];
    [self addSubview:_albumCollectionView];
    
    _albumCollectionView.delegate = self;
    _albumCollectionView.dataSource = self;
    
    [_albumCollectionView registerClass:[AlbumCollectionViewCell class] forCellWithReuseIdentifier:NSStringFromClass([AlbumCollectionViewCell class])];
    

}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {

//    NSLog(@"%ld", _imageArray.count);
    return self.imageArray.count;

}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

    AlbumCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([AlbumCollectionViewCell class]) forIndexPath:indexPath];
    NSNumber *selected = self.selectedArr[indexPath.row];
    cell.isSelected = selected.boolValue;
    cell.picImageView.image = self.imageArray[indexPath.row];
    [cell layoutSubviews];
    
    return cell;

}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    AlbumCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([AlbumCollectionViewCell class]) forIndexPath:indexPath];
    NSNumber *selectedNum = self.selectedArr[indexPath.row];
    cell.isSelected = !selectedNum.boolValue;
    if (!selectedNum.boolValue) {
        if (self.selectedCount >= self.allowSelectCount) {
            return;
        }
        else{
            self.selectedCount ++;
            cell.isSelected = !selectedNum.boolValue;
            [cell layoutSubviews];
            [self.selectedArr replaceObjectAtIndex:indexPath.row withObject:[NSNumber numberWithBool:cell.isSelected]];
            [self.albumCollectionView reloadData];

        }
    }
    else{
        self.selectedCount --;
        cell.isSelected = !selectedNum.boolValue;
        [cell layoutSubviews];
        [self.selectedArr replaceObjectAtIndex:indexPath.row withObject:[NSNumber numberWithBool:cell.isSelected]];
        [self.albumCollectionView reloadData];
    }
        NSArray *array = [PHKitTool getAllImageGroup];
    PHAsset *asset = array[indexPath.row];
//    NSLog(@"%lu, %lu", (unsigned long)asset.pixelWidth, (unsigned long)asset.pixelHeight);
//    if (self.isUseCutHead) {
//        UIImage *image = self.imageArray[indexPath.row];
//        [self.delegate sendImage:image];
//    }
//    else{
//        [self.delegate sendAsset:asset pixWidth:asset.pixelWidth pixHeight:asset.pixelHeight];
//    }
}




/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
